import { wxRequest } from '../../utils/server.js';
const _wxRequest = new wxRequest();
const app = getApp()
Page({ 
  data: {
    imgUrl: app.globalData.imgUrl,
    uploadUrl: wx.getStorageSync('uploadUrl'),
    goodlist: wx.getStorageSync('goodsOdrList'),
    odrTotalPrice: wx.getStorageSync('odrTotalPrice'),
    note:'',
    defaultAddress:{},
    id:''
  },
  onLoad:function(){
    this.getShopCartList();
    this.getDefaultAddress()
  },
  
  onShow: function (options) { 
    console.log(this.data.defaultAddress,'支付页面')
    this.setData({
      goodlist: wx.getStorageSync('goodsOdrList'),
      odrTotalPrice: wx.getStorageSync('odrTotalPrice')      
    }) 
  },

  //获取购物车列表
  getShopCartList() {
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/odr/protected/shoppingCart/list',
      method: 'POST',
      data: {}
    }, (res) => {
      this.setData({
        goodlist: res.list
      })
      wx.setStorageSync('goodsOdrList', res.list)
    })
  },

  // onUnload: function () {
  //   wx.removeStorageSync('goodsOdrList')
  //   wx.removeStorageSync('odrTotalPrice')
  // },

  // onHide: function () {
  //   wx.removeStorageSync('goodsOdrList')
  //   wx.removeStorageSync('odrTotalPrice')
  // },


  //监听input
  textareaBlur: function (e) { 
    var value = e.detail.value 
    this.setData({
      note:value
    })
  },

  //获取默认地址
  getDefaultAddress:function(){ 
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/odr/protected/customerAddress/defaultAddress',
      method: 'POST',
      data: {}
    }, (res) => {
      var resVo = res.vo
       this.setData({
         defaultAddress: resVo
       })
    })
  },

  //选择地址
  cheackAddress:function(){
    wx.navigateTo({
      url: '/pages/address/list?type=ord',
    })
  },
 


  //支付订单
  onClickButton:function(){ 
    this.odrRequest()
    wx.removeStorageSync('goodsOdrList')
    wx.removeStorageSync('odrTotalPrice')
  },

  //订单请求
  odrRequest:function(){
    if (!this.data.defaultAddress) {
      wx.showToast({
        icon: 'none',
        title: '请选择收货地址',
      })
      return false
    }
    
    var params={
      addId: this.data.defaultAddress.id,
      shoppingCartList:this.data.goodlist,
      note: this.data.note
    } 
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/odr/protected/order/submit2BOrder',
      method: 'POST',
      data: params
    }, (res) => { 
      wx.removeStorageSync('goodsOdrList')
      wx.removeStorageSync('odrTotalPrice')
      // wx.showToast({
      //   title: '支付成功',
      // })
      var list = res.list
      var ids = list.map(item => item.id) 
      console.log(ids,'ids')
      // var id = res.vo.id
      // this.setData({
      //   id
      // })
      //微信支付
      this.wxPay(ids) 
       console.log(res,'订单信息')
    })
  },



  //监听购物车数量
  handleCart(e) {
    var num = e.detail
    console.log(num);
    var key = e.target.dataset.key;
    var goodslist = this.data.goodlist
    var gdsItem = ''
    goodslist.map((item, index) => {
      if (index === key) {
        item.qty = num
        if(num > 0) {
          gdsItem = {
            id: Number(item.id),
            gdsId: Number(item.gdsId),
            qty: item.qty
          }
        }
        _wxRequest.wxTonkenAjax({
          url: app.globalData.apiUrl + '/odr/protected/shoppingCart/add',
          method: 'post',
          data: item
        }, (res) => {
          wx.setStorageSync('settleBack', true)
        })
      }
    })
    goodslist = goodslist.filter(item => item.qty > 0)
    this.setData({
      goodlist: goodslist,
      shoppingCartList: goodslist
    }) 
    this.hendleTotalPrice()
  },


  hendleTotalPrice() { //计算总价
    let that = this
    let price = 0
    for (let i = 0; i < that.data.shoppingCartList.length; i++) {
      // if (that.data.shoppingCartList[i].checked) {
      price = price + that.data.shoppingCartList[i].price * that.data.shoppingCartList[i].qty
      // }
    }
    price = price.toFixed(2)
    this.setData({
      odrTotalPrice: price
    })
  },


  //微信支付  
  wxPay: function (odrIdList) {
    var that = this;
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/odr/protected/order/wxPay',
      method: 'POST',
      data: odrIdList,
    }, function (res) {       
      that.requestPayment(res)
    })
  },

  //微信支付
  requestPayment: function (data) {
    var that = this;
    wx.requestPayment({
      'timeStamp': data.timeStamp,
      'nonceStr': data.nonceStr,
      'package': data.pkg,
      'signType': data.signType,
      'paySign': data.paySign,
      'success': function (res) {
        wx.showToast({
          icon: 'success',
          title: '支付成功',
      })
        setTimeout(function () {
          wx.redirectTo({
            // url: '/pages/settle/success?id=' + that.data.id,
            url:'/pages/order/index?status=2'
          })
        }, 1000)
      },
      'fail': function (res) {
        wx.showToast({
          icon: 'none',
          title: '支付失败',
        })
        wx.redirectTo({ 
          url: '/pages/order/index?status=1'
        })
      },
      'complete': function (res) {
        // that.payCallBack(data.outTradeNo)
      }
    })
  },




   
})